package baiqi.sishen.community.controller;

import baiqi.sishen.community.dto.NotificationDTO;
import baiqi.sishen.community.dto.QuestionDTO;
import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import baiqi.sishen.community.model.User;
import baiqi.sishen.community.service.NotificationService;
import baiqi.sishen.community.service.QuestionService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/4/30
 */
@Controller
public class ProfileController {

    @Resource
    private QuestionService questionService;

    @Resource
    private NotificationService notificationService;


    @GetMapping("/profile/{action}")
    public String profile(
            @PathVariable("action") String action,
            HttpServletRequest request,
            Model model,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "5") Integer size) {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.USER_NOT_FOUND);
        }
        if ("questions".equals(action)) {
            //当前登录者的问题
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的提问");
            PageInfo<QuestionDTO> questions = questionService.list(user.getId(), page, size);
            model.addAttribute("pagination", questions);
        } else if ("notifies".equals(action)) {
            //当前登录者的回复
            model.addAttribute("section", "notifies");
            model.addAttribute("sectionName", "我的回复");
            PageInfo<NotificationDTO> notifications = notificationService.list(user.getId(), page, size);
            model.addAttribute("pagination", notifications);
            //选中标识符
            model.addAttribute("navType", "notifyNav");
        } else if("likes".equals(action)) {
            //当前登录者的收藏
            model.addAttribute("section", "likes");
            model.addAttribute("sectionName", "我的收藏");
            PageInfo<QuestionDTO> likeQuestions = questionService.listLikes(user.getId(),page,size);
            model.addAttribute("pagination", likeQuestions);
        }

        return "profile";
    }
}
